﻿using Furion.RemoteRequest.Extensions;
using Microsoft.AspNetCore.Mvc;
using System.Text;

Serve.Run("http://*:1024");
[DynamicApiController]
public class HelloService
{
    ResultModel resultmodel;
    public HelloService()
    {
        resultmodel = new ResultModel()
        {
            Cmd = 0x01,
            ErrCode = 0x00,
            Message = "message消息"
        };
    }
    public async Task<string> Say()
    {
        //using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream, Encoding.UTF8))没有禁用bom 这边会回传 ef bb bf 是否需要特殊处理将bom去掉。
        var postResult = await "http://localhost:666".SetClientTimeout(10).SetBody(resultmodel).PostAsByteArrayAsync();
        return GetHexStringNoComma(postResult);
    }
    public async Task<string> Say1()
    {
        var postResult = await "http://localhost:666".SetClientTimeout(10).SetBody(resultmodel).PostAsStringAsync();
        return GetHexStringNoComma(Encoding.UTF8.GetBytes(postResult));
    }
    public async Task<string> Say2()
    {
        //stream内不存在bom
        var stream = await "http://localhost:666".SetClientTimeout(10).SetBody(resultmodel).PostAsStreamAsync();
        using var streamReader = new StreamReader(stream);
        var results = await streamReader.ReadToEndAsync();
        return GetHexStringNoComma(Encoding.UTF8.GetBytes(results));
    }
    public async Task<ResultModel> Say3()
    {
        return await "http://localhost:666".SetClientTimeout(10).SetBody(resultmodel).PostAsAsync<ResultModel>();
    }
    /// <summary>
    /// 通过bytes获取string
    /// </summary>
    /// <param name="FingerBytes"></param>
    /// <returns></returns>
    [NonAction]
    public static string GetHexStringNoComma(byte[] hex)
    {
        var retListstr = new List<string>();
        hex.ToList().ForEach(a =>
        {
            retListstr.Add(a.ToString("x2"));
        });
        return string.Join(" ", retListstr);
    }
}